var scoringData = {};  // 评分数据
var evaluationOneList = [];  // 存放一级评价
var evaluationTwoList = [];  // 存放二级评价
var scoreState = false;
layui.use(['rate'], function () {
    var rate = layui.rate;
    rate.render({//自定义文本
        elem: '#test',
        value: 0,
        text: true,
        setText: function (value) { //自定义文本的回调
            scoringData.value = value;   // 评分值
            var arr = {
                '1': '非常不满意，很差',
                '2': '一般，比较差',
                '3': '还行，还需改善',
                '4': '满意，认可改善',
                '5': '非常满意，无可挑剔'
            };
            this.span.text(arr[value] || ("您的评价会让我们做的更好"));
            var evaluate = [ //  1 - 4 星评价
                {id: 1, state: 1, value: '服务态度差'},
                {id: 2, state: 1, value: '治疗效果不满意'},
                {id: 3, state: 1, value: '环境卫生差'},
                {id: 4, state: 1, value: '术后医嘱不详细'},
                {id: 5, state: 1, value: '价格偏贵'},
                {id: 6, state: 1, value: '等候时间长'},
                {id: 7, state: 1, value: '治疗周期长'},
                {id: 8, state: 1, value: '治疗方案不清楚'},
                {id: 9, state: 1, value: '中途更换医生'},
                {id: 10, state: 1, value: '回访关心和沟通太少'},
                {id: 11, state: 2, value: '医护人员治疗过程中闲聊'},
                {id: 12, state: 2, value: '未指出口腔存在的其它问题'}
            ];
            var goodEvaluation = [//  5 星评价
                {id: 1, state: 1, value: '治疗效果好'},
                {id: 2, state: 1, value: '整体服务好'},
                {id: 3, state: 1, value: '卫生环境好'},
                {id: 4, state: 1, value: '医生技术好'}
            ];
            if (value !== 0) {
                $("#proposal").show(); // 显示建议框
                scoreState = true;
                $("#btn-selection").addClass('btn-selection');
            }
            var score = '';
            if (value === 1 || value === 2 || value === 3 || value === 4) {
                for (var e = 0; e < evaluate.length; e++) {
                    if (evaluate[e].state === 1) {
                        score += "<div class='score-option-item'>" + evaluate[e].value + "</div>";
                    } else {
                        score += "<div class='score-option-item' style='width: 100%;'>" + evaluate[e].value + "</div>";
                    }
                }
            } else if (value === 5) {
                for (var i = 0; i < goodEvaluation.length; i++) {
                    score += "<div class='score-option-item'>" + goodEvaluation[i].value + "</div>";
                }
            }
            $(".score-option-one").html(score);
            $(".score-option-evaluation").html("");
            evaluationOneList = [];
            evaluationTwoList = [];  // 切换的时候清空数组
        }
    })
});
var generalEvaluation = [//  治疗效果不满意评价延伸
    {id: 1, value: '种植'},
    {id: 2, value: '正畸'},
    {id: 3, value: '洁牙'},
    {id: 4, value: '美白'},
    {id: 5, value: '补牙'},
    {id: 6, value: '根管治疗'},
    {id: 7, value: '拔牙'},
    {id: 8, value: '烤瓷牙'},
    {id: 9, value: '嵌体'},
    {id: 10, value: '活动牙'},
    {id: 11, value: '隐形义齿'}
];
$(".score-option-one").on('click', '.score-option-item', function () {//  一级评价
    if ($(this).hasClass('score-option-item-selection')) {
        if ($(this).html() === '治疗效果不满意') {
            $(".score-option-evaluation").html("");
            evaluationTwoList = [];
        }
        $(this).removeClass('score-option-item-selection');
        evaluationOneList.splice($.inArray($(this).html(), evaluationOneList), 1);
    } else {
        if ($(this).html() === '治疗效果不满意') {
            var project = '';
            for (var i = 0; i < generalEvaluation.length; i++) {
                project += "<div class='score-option-item'>" + generalEvaluation[i].value + "</div>";
            }
            var projectHtml = $("<div class='score-option-title'>请问是对以下哪个项目的质量不满意？</div><div class='score-option-two'>" + project + "</div>");
            $(".score-option-evaluation").html(projectHtml);
        }
        $(this).addClass('score-option-item-selection');
        evaluationOneList.push($(this).html());
    }
});
$(".score-option-evaluation").on('click', '.score-option-item', function () {//  二级评价
    if ($(this).hasClass('score-option-item-selection')) {
        $(this).removeClass('score-option-item-selection');
        evaluationTwoList.splice($.inArray($(this).html(), evaluationTwoList), 1);
    } else {
        $(this).addClass('score-option-item-selection');
        evaluationTwoList.push($(this).html());
    }
});

var count = 0;
function submission_questionnaire() {//  提交
    count++;
    if (scoreState && count==1) {
        scoringData.proposal = $("#proposal").val();
        scoringData.evaluationOneList = evaluationOneList;
        scoringData.evaluationTwoList = evaluationTwoList;
        scoringData.openId = $("#openId").val();
        scoringData.caseid = $("#caseid").val();
        console.log(scoringData);
        $.ajax({
            url: Feng.ctxPath + "/integralMallWeixin/satisfactionSurveyAdd",
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(scoringData),
            success: function (data) {
                Feng.success('感谢您的评分！');
                window.location.href = Feng.ctxPath + "/integralMallWeixin/personalCenter" ;
            }
        });
    }
}